package com.haidechizi.fastadmin.modules.sys.controller;


import com.haidechizi.fastadmin.common.base.AbstractController;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.Constant;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.modules.sys.entity.SysOffice;
import com.haidechizi.fastadmin.modules.sys.service.SysOfficeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 部门管理
 */
@RestController
@RequestMapping("/sys/office")
public class SysOfficeController extends AbstractController {
    @Autowired
    private SysOfficeService sysOfficeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:office:list")
    public List<SysOffice> list(PageDto pageDto, SysOffice sysOffice) {
        List<SysOffice> officeList = sysOfficeService.queryList(pageDto, sysOffice);

        return officeList;
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:office:select")
    public Response select(PageDto pageDto, SysOffice sysOffice) {
        List<SysOffice> officeList = sysOfficeService.queryList(pageDto, sysOffice);

        //添加一级部门
        if (getUserId() == Constant.SUPER_ADMIN) {
            SysOffice root = new SysOffice();
            root.setOfficeId(0L);
            root.setName("一级部门");
            root.setParentId(-1L);
            root.setOpen(true);
            officeList.add(root);
        }

        return Response.ok().put("officeList", officeList);
    }

    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:office:list")
    public Response info(PageDto pageDto, SysOffice sysOffice) {
        long officeId = 0;
        if (getUserId() != Constant.SUPER_ADMIN) {
            List<SysOffice> officeList = sysOfficeService.queryList(pageDto, sysOffice);
            Long parentId = null;
            for (SysOffice office : officeList) {
                if (parentId == null) {
                    parentId = office.getParentId();
                    continue;
                }

                if (parentId > office.getParentId().longValue()) {
                    parentId = office.getParentId();
                }
            }
            officeId = parentId;
        }

        return Response.ok().put("officeId", officeId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{officeId}")
    @RequiresPermissions("sys:office:info")
    public Response info(@PathVariable("officeId") Long officeId) {
        SysOffice office = sysOfficeService.selectById(officeId);

        return Response.ok().put("office", office);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:office:save")
    public Response save(@RequestBody SysOffice office) {
        sysOfficeService.insert(office);

        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:office:update")
    public Response update(@RequestBody SysOffice office) {
        sysOfficeService.updateById(office);

        return Response.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:office:delete")
    public Response delete(long officeId) {
        //判断是否有子部门
        List<Long> officeList = sysOfficeService.queryOfficeIdList(officeId);
        if (officeList.size() > 0) {
            return Response.error("请先删除子部门");
        }

        sysOfficeService.deleteById(officeId);

        return Response.ok();
    }

}
